//
//  SubmitViewController.m
//  happyHealthyWalk
//
//  Created by newhuidong on 16/2/23.
//  Copyright © 2016年 HuiDong. All rights reserved.
//

#import "SubmitViewController.h"
#import "PellTableViewSelect.h"
#import "Header.h"
#import "BDImagePicker.h"
#define MAX_LIMIT_NUMS 140 //最大输入字符

//动画时间
#define kAnimationDuration .2

//view高度
#define kViewHeight 56

@interface SubmitViewController ()<UIScrollViewDelegate,UITextFieldDelegate,UITextViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scroller;

@property (weak, nonatomic) IBOutlet UITextField *title_TF;
@property (weak, nonatomic) IBOutlet UITextField *author_TF;

@property (weak, nonatomic) IBOutlet UIButton *section_button;
@property (weak, nonatomic) IBOutlet UILabel *time_label;
@property (weak, nonatomic) IBOutlet UITextView *body_TV;
@property (weak, nonatomic) IBOutlet UIButton *save_button;
@property (weak, nonatomic) IBOutlet UIButton *submit_button;
@property (weak, nonatomic) IBOutlet UIButton *reset_button;
@property (weak, nonatomic) IBOutlet UITableView *selected_tableview;
@end

@implementation SubmitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"在线投稿";
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];

    self.save_button.layer.cornerRadius=3;
    self.submit_button.layer.cornerRadius=3;
    self.reset_button.layer.cornerRadius=3;
    
    self.time_label.layer.cornerRadius=3;
    self.time_label.layer.borderColor=[UIColor grayColor].CGColor;
    self.time_label.layer.borderWidth=.5;
    
    
    self.body_TV.layer.cornerRadius=2;
    
//    self.scroller=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
//    
    self.scroller.contentSize=CGSizeMake(ScreenW, ScreenH*1.5);
//    self.scroller.backgroundColor=[UIColor redColor];
//    
//    [self.view addSubview:self.scroller];
//    
//    [self.view addSubview:self.title_TF];
//    [self.scroller addSubview:self.author_TF];
//    [self.scroller addSubview:self.section_button];
    
    [self setupKeyboard];
    
    self.section_button.layer.borderColor=[UIColor grayColor].CGColor;
    self.section_button.layer.cornerRadius=3;
    self.section_button.layer.borderWidth=.5;
    
    
    
}
- (IBAction)imagePick:(id)sender {
    
    [BDImagePicker showImagePickerFromViewController:self allowsEditing:YES finishAction:^(UIImage *image) {
       
        [sender setBackgroundImage:image forState:UIControlStateNormal];
        
        
    }];
    
    
}

#pragma textView delegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    return NO;
    
    
}


#pragma 键盘
-(void)setupKeyboard{
    
    UIToolbar * topView=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton =[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyboard)];
    NSArray * buttonsArray=[NSArray arrayWithObjects:btnSpace,doneButton, nil];
    [topView setItems:buttonsArray];
    
    [self.title_TF setInputAccessoryView:topView];
    [self.body_TV setInputAccessoryView:topView];
    [self.author_TF setInputAccessoryView:topView];
    
}
-(void)dismissKeyboard{
    [_title_TF resignFirstResponder];
    [_author_TF resignFirstResponder];
    [_author_TF resignFirstResponder];
    [_body_TV resignFirstResponder];
    
    
    
    
}
-(void)keyboardHide:(UITapGestureRecognizer*)tap{
    [self.scroller resignFirstResponder];
}

- (IBAction)sectionPick:(id)sender {
    
    NSLog(@"执行了");
    self.selected_tableview.hidden=NO;
    
        [UIView animateWithDuration:1.5f animations:^{
            self.selected_tableview.frame=CGRectMake(20, self.section_button.frame.origin.y+40, self.section_button.frame.size.width, 44*3);
            self.selected_tableview.layer.cornerRadius=3;
            self.selected_tableview.backgroundColor=[UIColor clearColor];
            
            self.selected_tableview.delegate=self;
            self.selected_tableview.dataSource=self;
            
        }];

        
    
    
    
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * identifer=@"cell";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    NSArray *array=@[@"从心出发",@"真情分享",@"你我相伴"];
    
    cell.textLabel.text=array[indexPath.row];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.font=[UIFont systemFontOfSize:15];
    
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        
        [self.section_button setTitle:@"从心出发" forState:0];
        
        self.selected_tableview.hidden=YES;
        
        
    }else if (indexPath.row==1){
        
        [self.section_button setTitle:@"真情分享" forState:0];
        self.selected_tableview.hidden=YES;

    }else{
        
        [self.section_button setTitle:@"你我相伴" forState:0];
        self.selected_tableview.hidden=YES;

        
    }
    
}
- (IBAction)save:(id)sender {
}
- (IBAction)submit:(id)sender {
    
    
    
}
- (IBAction)reset:(id)sender {
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.scroller endEditing:YES];
    
}
-(void)viewWillAppear:(BOOL)animated{
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];
    
    
}
-(void)keyboardDidShow:(NSNotification*)notification{
    
    NSValue * keyboardObject =[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect;
    
    [keyboardObject getValue:&keyboardRect];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:kAnimationDuration];
    [(UIView *)[self.view viewWithTag:1000]setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kViewHeight, 320, kViewHeight)];
    [UIView commitAnimations];
    
    
    
}
-(void)keyboardDidHidden{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:kAnimationDuration];
    [(UIView
      *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-kViewHeight, 320, kViewHeight)];
    
    [UIView
     commitAnimations];
    

}
@end
